package Code;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;


public class GnuPlotGenerator {
	private static final String TOTAL_FILE = "total.txt";
	private static final String ATTRACTION_FILE = "attraction.txt";
	private static final String REPULSION_FILE = "repulsion.txt";
	private static final String TANGENTIAL_FILE = "tangential.txt";
	private static final String RANDOM_FILE = "random.txt";
	
	
	private static final Point goal = new Point(-200, 200);
	private static int worldSize;
	private static List<Obstacle> obstacles;
	private static double modifyPotential;
	private static final int incr = 33;
	private static int start;
	
	public static void generateOutput4GnuPlot(int size, final List<Obstacle> _obstacles) {
		initVariables(size, _obstacles);
		
		try {
			outputAttractionFields();
			outputResistanceFields();
			outputTangentialFields();
			outputPotentialFields();
			outputRandomFields();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	private static void initVariables(int size, final List<Obstacle> _obstacles) {
		obstacles = _obstacles;
		worldSize = size;
		start = (int)(worldSize/incr)*incr/2;
		modifyPotential = 1;//100d/(worldSize/2);
	}
	
	private static void outputPotentialFields() throws FileNotFoundException, UnsupportedEncodingException 
	{
		final PrintWriter writer = new PrintWriter(TOTAL_FILE, "UTF-8");
		
		outputHeader(writer);
		outputObstacles(writer);
		Point potential;
		writer.println("plot '-' with vector head");
		for (int x = -1*start; x < worldSize/2; x += incr)
		{
			for (int y = -1*start; y < worldSize/2; y += incr)
			{
				potential = CalculatePotentials.calcPotentialPt(new Point (x, y), goal, obstacles, worldSize);
				writer.println(x + " " + y + " " + ((potential.getX()-x)*modifyPotential) + " " + 
						((potential.getY()-y)*modifyPotential));
			}
		}
		writer.close();
	}

	private static void outputTangentialFields() throws FileNotFoundException, UnsupportedEncodingException 
	{
		final PrintWriter writer = new PrintWriter(TANGENTIAL_FILE, "UTF-8");
		outputHeader(writer);
		outputObstacles(writer);
		Point tangential;
		writer.println("plot '-' with vector head");
		for (int x = -1*start; x < worldSize/2; x += incr)
		{
			for (int y = -1*start; y < worldSize/2; y += incr)
			{
				tangential = CalculatePotentials.calcTangential(new Point (x, y), obstacles, goal);
				writer.println(x + " " + y + " " + (tangential.getX()*modifyPotential) + " " + 
						(tangential.getY()*modifyPotential));
			}
		}
		writer.close();
	}

	private static void outputAttractionFields() throws FileNotFoundException, UnsupportedEncodingException 
    {
		final PrintWriter writer = new PrintWriter(ATTRACTION_FILE, "UTF-8");
		
        outputHeader(writer);
        outputObstacles(writer);
        Point attraction;
        writer.println("plot '-' with vector head");
        for (int x = -1*start; x < worldSize/2; x += incr)
        {
                for (int y = -1*start; y < worldSize/2; y += incr)
                {
                        attraction = CalculatePotentials.calcAttraction(new Point (x, y), goal);
                        writer.println(x + " " + y + " " + (attraction.getX()*modifyPotential) + " " + 
                                        (attraction.getY()*modifyPotential));
                }
        }
        
        writer.close();
	}
	
	private static void outputResistanceFields() throws FileNotFoundException, UnsupportedEncodingException 
	{
		final PrintWriter writer = new PrintWriter(REPULSION_FILE, "UTF-8");
		
		 outputHeader(writer);
		 outputObstacles(writer);
		 Point resistance;
		 writer.println("plot '-' with vector head");
		 for (int x = -1*start; x < worldSize/2; x += incr)
		 {
			 for (int y = -1*start; y < worldSize/2; y += incr)
			 {
				 resistance = CalculatePotentials.calcResistance(new Point (x, y), obstacles);
				 writer.println(x + " " + y + " " + (resistance.getX()*modifyPotential) + " " + 
						 (resistance.getY()*modifyPotential));
			 }
		 }
		 writer.close();
	}

	private static void outputRandomFields() throws FileNotFoundException, UnsupportedEncodingException 
	{
		final PrintWriter writer = new PrintWriter(RANDOM_FILE, "UTF-8");
		
		 outputHeader(writer);
		 outputObstacles(writer);
		 Point random;
		 writer.println("plot '-' with vector head");
		 for (int x = -1*start; x < worldSize/2; x += incr)
		 {
			 for (int y = -1*start; y < worldSize/2; y += incr)
			 {
				 random = CalculatePotentials.calcRandom(new Point (x, y), worldSize);
				 writer.println(x + " " + y + " " + (random.getX()*modifyPotential) + " " + 
						 (random.getY()*modifyPotential));
			 }
		 }
		 writer.close();
	}
	
	//output the obstacles for gnuplot
	private static void outputObstacles(PrintWriter writer) 
	{
		writer.println("unset arrow");
		for(Obstacle obst: obstacles)
		{
			for (int i = 0; i < 4; i++)
			{
				writer.print("set arrow from ");
				writer.print(obst.getCorner(i).getX() + ", " + obst.getCorner(i).getY());
				writer.print(" to " + obst.getCorner((i+1)%4).getX() + ", ");
				writer.println(obst.getCorner((i+1)%4).getY() + " nohead lt 3");
				
			}
		}
		writer.println();
	}

	//Output gnuplot header info
	private static void outputHeader(PrintWriter writer) 
	{
		writer.println("set xrange [" + (-1*worldSize/2) + ": " + (worldSize/2) + "]");
		writer.println("set yrange [-400.0: 400.0]");
		writer.println("unset key");
		writer.println("set size square");
		writer.println();
	}
	
	
	
	
	
}
